package com.sp.system.user.controller;

import com.sp.system.user.core.utils.ContentResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;

@Slf4j
@RestController
public class UserController {
    @Resource
    private ConsumerTokenServices consumerTokenServices;

    @GetMapping("/user")
    public Authentication user(Principal user) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.debug("oauth2 user:{}", authentication.getName());
        return authentication;
    }

    @GetMapping("/ti/removeToken")
    public Object removeToken(String accessToken) {
        if (consumerTokenServices.revokeToken(accessToken)) {
            return ContentResult.successResult(null, "注销成功");
        }
        return ContentResult.errorResult(-1, "注销失败");
    }
}